//
//  NativeAdView.m
//  DualMediationSDK
//
//  Created by YeJary on 2025/7/10.
//


#import "NativeAdView.h"

@interface NativeAdView ()

@property (nonatomic, strong) ISNativeAdView *nativeAdView;
@property (nonatomic, strong) UIImageView    *adAppIcon;
@property (nonatomic, strong) UILabel        *adTitleView;
@property (nonatomic, strong) UILabel        *adAdvertiserView;
@property (nonatomic, strong) UILabel        *adBodyView;
@property (nonatomic, strong) LevelPlayMediaView *adMediaView;
@property (nonatomic, strong) UIButton       *adCallToActionView;

@end

@implementation NativeAdView

- (instancetype)initWithNativeAd:(LevelPlayNativeAd *)nativeAd {
    if (self = [super initWithFrame:CGRectZero]) {
        [self setupSubviews];
        [self populateWithContent:nativeAd];
    }
    return self;
}

- (void)setupSubviews {
    self.translatesAutoresizingMaskIntoConstraints = NO;

    // 1. 容器：ISNativeAdView
    self.nativeAdView = [[ISNativeAdView alloc] initWithFrame:CGRectZero];
    self.nativeAdView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.nativeAdView];
    [NSLayoutConstraint activateConstraints:@[
        [self.nativeAdView.topAnchor constraintEqualToAnchor:self.topAnchor],
        [self.nativeAdView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
        [self.nativeAdView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
        [self.nativeAdView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor]
    ]];

    // 2. Icon
    self.adAppIcon = [[UIImageView alloc] init];
    self.adAppIcon.translatesAutoresizingMaskIntoConstraints = NO;
    [self.nativeAdView addSubview:self.adAppIcon];

    // 3. Title
    self.adTitleView = [[UILabel alloc] init];
    self.adTitleView.font = [UIFont boldSystemFontOfSize:16];
    self.adTitleView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.nativeAdView addSubview:self.adTitleView];

    // 4. Advertiser
    self.adAdvertiserView = [[UILabel alloc] init];
    self.adAdvertiserView.font = [UIFont systemFontOfSize:12];
    self.adAdvertiserView.textColor = [UIColor grayColor];
    self.adAdvertiserView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.nativeAdView addSubview:self.adAdvertiserView];

    // 5. MediaView
    self.adMediaView = [[LevelPlayMediaView alloc] initWithFrame:CGRectZero];
    self.adMediaView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.nativeAdView addSubview:self.adMediaView];

    // 6. Body
    self.adBodyView = [[UILabel alloc] init];
    self.adBodyView.font = [UIFont systemFontOfSize:14];
    self.adBodyView.numberOfLines = 2;
    self.adBodyView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.nativeAdView addSubview:self.adBodyView];

    // 7. CTA Button
    self.adCallToActionView = [UIButton buttonWithType:UIButtonTypeSystem];
    self.adCallToActionView.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    self.adCallToActionView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.nativeAdView addSubview:self.adCallToActionView];

    // 布局约束
    [NSLayoutConstraint activateConstraints:@[
        // icon
        [self.adAppIcon.topAnchor constraintEqualToAnchor:self.nativeAdView.topAnchor constant:8],
        [self.adAppIcon.leadingAnchor constraintEqualToAnchor:self.nativeAdView.leadingAnchor constant:8],
        [self.adAppIcon.widthAnchor constraintEqualToConstant:40],
        [self.adAppIcon.heightAnchor constraintEqualToConstant:40],

        // title
        [self.adTitleView.topAnchor constraintEqualToAnchor:self.nativeAdView.topAnchor constant:8],
        [self.adTitleView.leadingAnchor constraintEqualToAnchor:self.adAppIcon.trailingAnchor constant:8],
        [self.adTitleView.trailingAnchor constraintEqualToAnchor:self.nativeAdView.trailingAnchor constant:-8],

        // advertiser
        [self.adAdvertiserView.topAnchor constraintEqualToAnchor:self.adTitleView.bottomAnchor constant:4],
        [self.adAdvertiserView.leadingAnchor constraintEqualToAnchor:self.adTitleView.leadingAnchor],
        [self.adAdvertiserView.trailingAnchor constraintEqualToAnchor:self.nativeAdView.trailingAnchor constant:-8],

        // media
        [self.adMediaView.topAnchor constraintEqualToAnchor:self.adAppIcon.bottomAnchor constant:8],
        [self.adMediaView.leadingAnchor constraintEqualToAnchor:self.nativeAdView.leadingAnchor constant:8],
        [self.adMediaView.trailingAnchor constraintEqualToAnchor:self.nativeAdView.trailingAnchor constant:-8],
        [self.adMediaView.heightAnchor constraintEqualToConstant:80],

        // body
        [self.adBodyView.topAnchor constraintEqualToAnchor:self.adMediaView.bottomAnchor constant:8],
        [self.adBodyView.leadingAnchor constraintEqualToAnchor:self.nativeAdView.leadingAnchor constant:8],
        [self.adBodyView.trailingAnchor constraintEqualToAnchor:self.nativeAdView.trailingAnchor constant:-8],

        // cta
        [self.adCallToActionView.topAnchor constraintEqualToAnchor:self.adBodyView.bottomAnchor constant:8],
        [self.adCallToActionView.centerXAnchor constraintEqualToAnchor:self.nativeAdView.centerXAnchor],
        [self.adCallToActionView.bottomAnchor constraintEqualToAnchor:self.nativeAdView.bottomAnchor constant:-8]
    ]];
}

- (void)populateWithContent:(LevelPlayNativeAd *)nativeAd {
    // 异步加载 icon
    NSURL *iconURL = nativeAd.icon.url;
    if (iconURL) {
        [[[NSURLSession sharedSession] dataTaskWithURL:iconURL completionHandler:^(NSData *d, NSURLResponse *r, NSError *e) {
            if (d) dispatch_async(dispatch_get_main_queue(), ^{
                self.adAppIcon.image = [UIImage imageWithData:d];
            });
        }] resume];
    }
    self.adTitleView.text      = nativeAd.title;
    self.adAdvertiserView.text = nativeAd.advertiser;
    self.adBodyView.text       = nativeAd.body;
    [self.adCallToActionView setTitle:nativeAd.callToAction forState:UIControlStateNormal];
    self.adCallToActionView.userInteractionEnabled = NO;

    // 注册曝光与点击：在 nativeAdView 上调用
    [self.nativeAdView registerNativeAdViews:nativeAd];
}


@end
